#ifndef __HAPTIC_H__
#define __HAPTIC_H__

#include "main.h"

#define DEF_ADDR    (0x4A<<1)
#define CHIP_REV    0xBA    //default is 0xBA
#define ENABLE      0x01
#define UNLOCKED    0x01
#define DISABLE     0x00
#define LOCKED      0x00
#define LRA_TYPE    0x00
#define ERM_TYPE    0x01
#define RAMP        0x01
#define STEP        0x00

struct hapticSettings {
  uint8_t motorType; 
  float nomVolt;
  float absVolt; 
  float currMax;
  float impedance; 
  float lraFreq;  

};

typedef enum {

  HAPTIC_SUCCESS,
  E_SEQ_CONTINUE = 0x01,
  E_UVLO = 0x02,
  HAPTIC_HW_ERROR,
  E_SEQ_DONE = 0x04,
  HAPTIC_INCORR_PARAM,
  HAPTIC_UNKNOWN_ERROR,
  E_OVERTEMP_CRIT = 0x08,
  E_SEQ_FAULT = 0x10,
  E_WARNING = 0x20,
  E_ACTUATOR_FAULT = 0x40,
  E_OC_FAULT = 0x80

} event_t;

typedef enum {

  NO_DIAG = 0x00,
  E_PWM_FAULT = 0x20,
  E_MEM_FAULT = 0x40,
  E_SEQ_ID_FAULT = 0x80

} diag_status_t;

typedef enum {

  STATUS_NOM = 0x00,
  STA_SEQ_CONTINUE = 0x01,
  STA_UVLO_VBAT_OK = 0x02,
  STA_PAT_DONE = 0x04,
  STA_OVERTEMP_CRIT = 0x08,
  STA_PAT_FAULT = 0x10,
  STA_WARNING = 0x20,
  STA_ACTUATOR = 0x40,
  STA_OC = 0x80

} status_t;

enum OPERATION_MODES {

   INACTIVE  = 0x00, 
   DRO_MODE, 
   PWM_MODE,
   RTWM_MODE, 
   ETWM_MODE

};

enum SNPMEM_ARRAY_POS {

  BEGIN_SNP_MEM = 0x00,
  NUM_SNIPPETS = 0x00,
  NUM_SEQUENCES = 0x01,
  ENDPOINTERS = 0x02,
  SNP_ENDPOINTERS = 0x02,  
  //SEQ_ENDPOINTERS = 0x11, //SNP_ENDPOINTERS + 14 = 0x10
  TOTAL_MEM_REGISTERS = 0x64,

};

enum SNPMEM_REGS {

  NUM_SNIPPETS_REG = 0x84,
  NUM_SEQUENCES_REG = 0x85,
  SNP_ENDPOINTERS_REGS = 0x88, // Up to 15 endpointers can be addressed
  //SEQ_ENDPOINTERS_REGS = 0x89,
  END_OF_MEM = 0xE7 // 0x84 + 99 = 0xE7
};

enum REGISTERS {

  CHIP_REV_REG = 0x00, //whoami?
  IRQ_EVENT1 = 0x03, 
  IRQ_EVENT_WARN_DIAG, 
  IRQ_EVENT_SEQ_DIAG, 
  IRQ_STATUS1, 
  IRQ_MASK1, 
  CIF_I2C1, 
  FRQ_LRA_PER_H = 0x0A, 
  FRQ_LRA_PER_L, 
  ACTUATOR1, 
  ACTUATOR2, 
  ACTUATOR3, 
  CALIB_V2I_H, 
  CALIB_V2I_L = 0x10, 
  CALIB_IMP_H, 
  CALIB_IMP_L, 
  TOP_CFG1,
  TOP_CFG2,
  TOP_CFG3,
  TOP_CFG4,
  TOP_INT_CFG1,
  TOP_INT_CFG6_H = 0x1C,
  TOP_INT_CFG6_L,
  TOP_INT_CFG7_H,
  TOP_INT_CFG7_L,
  TOP_INT_CFG8 = 0x20,
  TOP_CTL1 = 0x22,
  TOP_CTL2,
  SEG_CTL1,
  SWG_C1,
  SWG_C2,
  SWG_C3,
  SEQ_CTL2,
  GPI_0_CTL,
  GPI_1_CTL,
  GPI_2_CTL,
  MEM_CTL1,
  MEM_CTL2,
  ADC_DATA_H1,
  ADC_DATA_L1,
  POLARITY = 0x43,
  LRA_AVR_H,
  LRA_AVR_L,
  FRQ_LRA_PER_ACT_H,
  FRQ_LRA_PER_ACT_L,
  FRQ_PHASE_H,
  FRQ_PHASE_L,
  FRQ_CTL = 0x4C,
  TRIM3 = 0x5F,
  TRIM4,
  TRIM6 = 0x62,
  TOP_CFG5 = 0x6E,
  IRQ_EVENT_ACTUATOR_FAULT = 0x81,
  IRQ_STATUS2,
  IRQ_MASK2,
  SNP_MEM_X

};


/*Confirm The Device.*/
ErrorStatus Confirm_Device(uint8_t reg);
/*Initialize the device.*/
ErrorStatus Init_Motor(void);
/*Set the motor type.*/
ErrorStatus Set_Motor_Type(uint8_t type);
/*Set the absolute maximum rated voltage.*/
ErrorStatus Set_ABSVolt(float absvolt);
/*Set nominal voltage*/
ErrorStatus Set_NomVolt(float nomvolt);
/*Set the maximum rated current.*/
ErrorStatus Set_MaxCurrent(float maxcurrent);
/*Set the impedance.*/
ErrorStatus Set_Impedance(float impedance);
/*Set motor frequency.*/
ErrorStatus Set_Frequency(float frequency);
/*Enable resonant frequency tracking*/
ErrorStatus Enable_FreqTrack(FlagStatus enable);
/*Set the motor mode*/
ErrorStatus Set_OperatingModes(uint8_t mode);
/*Set the output drive level in DRO mode.*/
ErrorStatus Set_Vibrate(uint8_t value);




/*Write the register*/
ErrorStatus Write_Register(uint8_t reg, uint8_t mask, uint8_t bit, uint8_t position);



#endif




